package com.xy.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.xy.bean.Yellow;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ActiveProfiles;

import javax.sql.DataSource;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/4/9 8:32
 * @description
 * @Profile
 * spring 提供根据环境动态激活和bean的功能
 * 开发环境、测试环境、生产环境
 * 数据源 A、B、C
 */


@Configuration
@PropertySource("classpath:druid.properties")
public class MainConfigOfProfile {
    @Value("${druid.driver}")
    private  String driver;
    @Value("${druid.url}")
    private String url;
    @Value("${druid.username}")
    private String username;
    @Value("${druid.password}")
    private String password;

    @Bean
    public Yellow yellow() {
        return new Yellow();
    }


    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSourceTest() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url+ "spring_test");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Profile("dev")
    @Bean("devDataSource")
    public DataSource dataSourceDev() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url+ "spring_dev");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Profile("prod")
    @Bean("prodDataSource")
    public DataSource dataSourceProd() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url + "spring_prod");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
